import {
  createRouter,
  createWebHistory,
  createWebHashHistory,
} from 'vue-router'

const mode = import.meta.env.VITE_ROUTER_MODE

const routerMode = {
  hash: () => createWebHashHistory(),
  history: () => createWebHistory(),
}

const router = createRouter({
  history: routerMode[mode](),
  strict: false,
  scrollBehavior: () => ({ left: 0, top: 0 }),
  routes: [
    {
      path: '/',
      component: () => import('@/layout/index.vue'),
      children: [
        {
          path: '/',
          name: 'home',
          component: () => import('@/pages/index.vue'),
        },
        {
          path: '/details/:name',
          name: 'details',
          component: () => import('@/pages/details.vue'),
        },
        {
          path: '/explore',
          name: 'explore',
          component: () => import('@/pages/explore.vue'),
        },
        {
          path: '/my',
          name: 'my',
          component: () => import('@/pages/my.vue'),
        },
        {
          path: '/create-role',
          name: 'create-role',
          component: () => import('@/pages/create-role.vue'),
        },
        {
          path: '/testvoice',
          name: 'testvoice',
          component: () => import('@/pages/testvoice.vue'),
        },
      ],
    },
  ],
})

// 按需启动
// router.beforeEach((to, from, next) => {
//   // 优先从 query 里取 env，没有则从 localStorage 取
//   const env = to.query.env
//   // 存储到 localStorage，刷新后依然生效
//   if (env === 'prod') {
//     instance.defaults.baseURL = 'https://translate.luckyremoter.com/app'
//   } else {
//     instance.defaults.baseURL =
//       'https://testenv.toponegames.cn/topone-webapi-sdk/'
//   }
//   next()
// })

export default router
